/*******************************************************************************
 * Copyright (c) 2002 Boris Pruessmann and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Common Public License v0.5 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.core.api;

import com.google.common.collect.Maps;

import net.sourceforge.perforce.core.internal.Policy;

import java.util.Map;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
public final class ResolveTypeEnum {
  //----------------------------------------------------------------------------
  // Public Constants

  /** ResolveTypeEnum: Accept Theirs */
  public static final ResolveTypeEnum ACCEPT_THEIRS = new ResolveTypeEnum(0, "Accept Theirs"); //$NON-NLS-1$
  /** ResolveTypeEnum: Accept Yours */
  public static final ResolveTypeEnum ACCEPT_YOURS = new ResolveTypeEnum(1, "Accept Yours"); //$NON-NLS-1$
  /** ResolveTypeEnum: Safe Automatic Resolve */
  public static final ResolveTypeEnum SAFE_AUTOMATIC =
      new ResolveTypeEnum(2, "Safe Automatic Resolve"); //$NON-NLS-1$
  /** ResolveTypeEnum: Automatic Resolve */
  public static final ResolveTypeEnum AUTOMATIC = new ResolveTypeEnum(3, "Automatic Resolve"); //$NON-NLS-1$
  /** ResolveTypeEnum: Automatic Resolve Including Conflict Markers */
  public static final ResolveTypeEnum AUTOMATIC_MARKERS =
      new ResolveTypeEnum(4, "Automatic Resolve Including Conflict Markers"); //$NON-NLS-1$

  // --------------------------------------------------------------------
  // Private Static Variables

  /** Used to map the enum's id to the singleton instance. */
  private static Map<Integer, ResolveTypeEnum> idToEnumMap;

  // ---------------------------------------------------------------------------
  // Private Variables

  /** Stores the enum's id. */
  private int id;
  /** Stores the enum's description. */
  private String description;

  // ------------------------------------------------------------------------
  // Private Constructors

  /**
   * Constructor for ResolveTypeEnum.
   * 
   * @param theId the unique id of the enum value.
   * @param theDescription the description.
   */
  private ResolveTypeEnum(int theId, String theDescription) {
    if (idToEnumMap == null) {
      idToEnumMap = Maps.newHashMap();
    }

    // ==( PRE-CONDITION
    // )======================================================================
    Policy.assertTrue(!idToEnumMap.containsKey(new Integer(theId)));
    // ==( PRE-CONDITION
    // )======================================================================

    id = theId;
    description = theDescription;

    idToEnumMap.put(new Integer(theId), this);
  }

  // -----------------------------------------------------------------------
  // Public Static Methods

  /**
   * Returns the ResolveTypeEnum with the given id.
   * 
   * @param id the identifier of the enum value.
   * @return the enum.
   */
  public static ResolveTypeEnum fromId(int id) {
    ResolveTypeEnum result = idToEnumMap.get(new Integer(id));

    // ==( POST-CONDITION
    // )=====================================================================
    Policy.assertNotNull(result);
    // ==( POST-CONDITION
    // )=====================================================================
    return result;
  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  /**
   * Returns the id of the enum.
   * 
   * @return the id of the enum.
   */
  public int getId() {
    return id;
  }

  @Override
  public String toString() {
    return description;
  }

  /**
   * We only provide equals and not hashCode because the implementation of
   * equals is exactly like the one on the super class. However, this one is
   * faster.
   * 
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object obj) {
    return this == obj;
  }
}
